# Maintainer: Rafał Brzegowy <rafal.brzegowy@yahoo.com>

_realname=frontistr
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.4
pkgrel=2
pkgdesc='Open-Source Large-Scale Parallel FEM Program for Nonlinear Structural Analysis (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" 
         $([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-openmp") 
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-parmetis"
         "${MINGW_PACKAGE_PREFIX}-metis"
         "${MINGW_PACKAGE_PREFIX}-msmpi"
         "${MINGW_PACKAGE_PREFIX}-mumps"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-seacas"
         "${MINGW_PACKAGE_PREFIX}-trilinos"
         "${MINGW_PACKAGE_PREFIX}-doxygen"
         "${MINGW_PACKAGE_PREFIX}-scalapack"
         "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
makedepends=("${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran")
license=('MIT')
url="https://www.frontistr.com"
source=("https://github.com/${_realname}/${_realname}/archive/refs/tags/v${pkgver}.tar.gz")
sha256sums=('e98d5e8e4937bf9be8d331ed223cdea19c1ea923bafc0801113773d8d0663111')

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_Fortran_FLAGS="-fallow-invalid-boz" \
      -DWITH_MPI=ON \
      -DWITH_ML=ON \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
  
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} cmake --build . --target install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/License.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE 

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in $(find "${pkgdir}${MINGW_PREFIX}"/lib/cmake -type f); do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
  
}
